/**
 * @Date 2025/1/2 8:57
 * @Created by 衡盛永
 */

package org.example.deploy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.deploy.mapper.SysMenuMapper;
import org.example.deploy.mapper.SysUserMapper;
import org.example.deploy.pojo.SysMenu;
import org.example.deploy.pojo.SysUser;
import org.example.deploy.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    // 继承了 BaseService 和 ServiceImpl 的所有方法
    // 这里可以编写业务逻辑代码
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public static List<SysMenu> buildTree(List<SysMenu> menuList) {
        Map<Integer, SysMenu> menuMap = new HashMap<>();
        List<SysMenu> rootMenus = new ArrayList<>();

        // 将菜单按 id 存储到 map 中
        for (SysMenu menu : menuList) {
            menuMap.put(menu.getId(), menu);
        }

        // 生成树结构
        for (SysMenu menu : menuList) {
            if (menu.getParentId() == null) {
                // 顶级菜单，添加到 rootMenus
                rootMenus.add(menu);
            } else {
                // 非顶级菜单，添加到对应父菜单的 children 列表中
                SysMenu parentMenu = menuMap.get(menu.getParentId());
                if (parentMenu != null) {
                    parentMenu.getChildren().add(menu);
                }
            }
        }
        return rootMenus;
    }

    @Override
    public List<SysMenu> getMenusByUserId() {
        // Shiro
//        String username = (String) SecurityUtils.getSubject().getPrincipal();
        String username = "zs";
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>().eq("username", username);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        Integer roleId = sysUserMapper.getRoleIdByUserId(sysUser.getId());
        List<Integer> menuIdsByRoleId = sysMenuMapper.getMenuIdsByRoleId(roleId);
        List<SysMenu> menuListByMenuIds = sysMenuMapper.getMenuListByMenuIds(menuIdsByRoleId);
        return buildTree(menuListByMenuIds);
    }

    @Override
    public List<SysMenu> getAllMenus() {
        return buildTree(sysMenuMapper.selectList(null));
    }

    @Override
    @Transactional
    public Boolean assignMenu(Integer roleId, List<Integer> menuIds) {
        // 获取当前角色拥有的菜单权限
        List<Integer> menuIdsByRoleId = sysMenuMapper.getMenuIdsByRoleId(roleId);
        // 删除当前角色拥有的菜单权限
        Boolean b = sysMenuMapper.delAssignedMenus(roleId, menuIdsByRoleId);
        // 重新给当前角色分配菜单权限
        boolean b1 = sysMenuMapper.assignMenu(roleId, menuIds);
        return true;
    }
}